/*
 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
 */

#ifndef __UVOICE_OS_H__
#define __UVOICE_OS_H__


#include <k_api.h>
#include <aos/kernel.h>
#include <aos/kv.h>
#include <aos/list.h>
#include <aos/vfs.h>
//#include <aos/yloop.h>
#include <aos/hal/flash.h>

#include "../../../3rdparty/experimental/fs/fatfs/ff/include/ff.h"

#include "ulog/ulog.h"

#include "hal/wifi.h"

typedef aos_dir_t		os_dir_t;
typedef aos_dirent_t 	os_dirent_t;

typedef aos_queue_t		os_queue_t;
typedef aos_mutex_t		os_mutex_t;
typedef aos_sem_t		os_sem_t;
typedef aos_task_t 		os_task_t;
typedef aos_timer_t		os_timer_t;
typedef dlist_t			os_list_t;

#define OS_WAIT_FOREVER		AOS_WAIT_FOREVER

#define OS_LIST_INIT					AOS_DLIST_INIT
#define os_list_for_each_entry			dlist_for_each_entry
#define os_list_for_each_entry_safe		dlist_for_each_entry_safe


#ifndef MIN
#define MIN(a,b) ((a)<(b) ? (a):(b))
#endif

#ifndef MAX
#define MAX(a,b) ((a)>(b) ? (a):(b))
#endif

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

#ifndef bool
#define bool	char
#endif

#ifndef true
#define true	1
#endif

#ifndef false
#define false	0
#endif

#define snd_memcpy(dest, src, length) 		\
	{										\
		if ((dest) != (src) && length > 0)	\
			memcpy(dest, src, length);		\
	}

#define snd_memmove(dest, src, length) 		\
	{										\
		if ((dest) != (src))				\
			memmove(dest, src, length);		\
	}

enum {
	UVOICE_TASK_PRI_IDLE		= RHINO_IDLE_PRI,
	UVOICE_TASK_PRI_LOWEST		= AOS_DEFAULT_APP_PRI,
	UVOICE_TASK_PRI_LOWER		= AOS_DEFAULT_APP_PRI - 1,
	UVOICE_TASK_PRI_NORMAL		= AOS_DEFAULT_APP_PRI - 2,
	UVOICE_TASK_PRI_HIGHER		= AOS_DEFAULT_APP_PRI - 3,
	UVOICE_TASK_PRI_HIGHEST		= RHINO_CONFIG_TIMER_TASK_PRI + 1,
	UVOICE_TASK_PRI_REALTIME	= RHINO_CONFIG_TIMER_TASK_PRI
};
#include "cli_cmd.h"
#define M_LOGD	
#define M_LOGI	
#define M_LOGW	
#define M_LOGE	

#define AFM_MAIN		0x1
#define AFM_EXTN		0x2

static inline void *snd_zalloc(size_t size, int flags)
{
	void *mem = malloc(size);
	if (mem)
		memset(mem, 0, size);
	return mem;
}

static inline void snd_free(void *mem)
{
	free(mem);
}

static inline void *snd_realloc(void *old, size_t newsize, int flags)
{
	void *mem = realloc(old, newsize);
	if (mem)
		memset(mem, 0, newsize);
	return mem;
}

static inline int os_free_heap_bytes(void)
{
	return g_kmm_head->free_size;
}

static inline void os_msleep(int msec)
{
	aos_msleep(msec);
}

static inline long long os_current_time(void)
{
	return aos_now_ms();
}

static inline int os_get_mac_address(char *mac)
{
	uint8_t mac_addr[6];

	memset(mac_addr, 0, sizeof(mac_addr));
	if (hal_wifi_get_mac_addr(NULL, mac_addr))
		return -1;

	snprintf(mac, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
		mac_addr[0], mac_addr[1], mac_addr[2],
		mac_addr[3], mac_addr[4], mac_addr[5]);
	return 0;
}

static inline void os_list_add(os_list_t *node, os_list_t *head)
{
	dlist_add(node, head);
}

static inline void os_list_add_tail(os_list_t *node, os_list_t *head)
{
	dlist_add_tail(node, head);
}

static inline int os_list_entry_count(os_list_t *head)
{
	return dlist_entry_number(head);
}

static inline int os_list_empty(os_list_t *head)
{
	return dlist_empty(head);
}

static inline void os_list_del(os_list_t *node)
{
	dlist_del(node);
}

static inline void os_list_init(os_list_t *head)
{
	dlist_init(head);
}

static inline int os_kv_get(const char *key, void *buffer, int *len)
{
	return aos_kv_get(key, buffer, len);
}

static inline int os_kv_set(const char *key, const void *val, int len, int sync)
{
	return aos_kv_set(key, val, len, sync);
}

static inline int os_mkdir(const char *path)
{
	return aos_mkdir(path);
}

static inline os_dir_t *os_opendir(const char *path)
{
	return aos_opendir(path);
}

static inline os_dirent_t *os_readdir(os_dir_t *dir)
{
	return aos_readdir(dir);
}

static inline int os_closedir(os_dir_t *dir)
{
	return aos_closedir(dir);
}


static inline int os_queue_send(os_queue_t *queue, void *msg, unsigned int size)
{
	return aos_queue_send(queue, msg, size);
}

static inline int os_queue_recv(os_queue_t *queue, unsigned int ms, void *msg,
                   unsigned int *size)
{
	return aos_queue_recv(queue, ms, msg, size);
}

static inline int os_queue_new(os_queue_t *queue, void *buffer, unsigned int size, int msg_size)
{
	return aos_queue_new(queue, buffer, size, msg_size);
}

static inline void os_queue_free(os_queue_t *queue)
{
	aos_queue_free(queue);
}

static inline int os_event_post(uint16_t type, uint16_t code, int value)
{
	return 0;//aos_post_event(type, code, value);
}

static inline int os_event_register(uint16_t type, void *cb, void *data)
{
	return 0;//aos_register_event_filter(type, (aos_event_cb)cb, data);
}

static inline int os_event_unregister(uint16_t type, void *cb, void *data)
{
	return 0;//aos_unregister_event_filter(type, (aos_event_cb)cb, data);
}

static inline int os_mutex_lock(os_mutex_t *mutex, unsigned int timeout)
{
	return aos_mutex_lock(mutex, timeout);
}

static inline int os_mutex_unlock(os_mutex_t *mutex)
{
	return aos_mutex_unlock(mutex);
}

static inline int os_mutex_new(os_mutex_t *mutex)
{
	return aos_mutex_new(mutex);
}

static inline void os_mutex_free(os_mutex_t *mutex)
{
	aos_mutex_free(mutex);
}

static inline int os_sem_is_valid(os_sem_t *sem)
{
	return aos_sem_is_valid(sem);
}

static inline int os_sem_wait(os_sem_t *sem, unsigned int timeout)
{
	return aos_sem_wait(sem, timeout);
}

static inline void os_sem_signal(os_sem_t *sem)
{
	aos_sem_signal(sem);
}

static inline void os_sem_signal_all(os_sem_t *sem)
{
	aos_sem_signal_all(sem);
}

static inline int os_sem_new(os_sem_t *sem, int count)
{
	return aos_sem_new(sem, count);
}

static inline void os_sem_free(os_sem_t *sem)
{
	aos_sem_free(sem);
}

static inline int os_timer_change(os_timer_t *timer, int internal_ms)
{
	return aos_timer_change(timer, internal_ms);
}

static inline int os_timer_start(os_timer_t *timer)
{
	return aos_timer_start(timer);
}

static inline int os_timer_stop(os_timer_t *timer)
{
	return aos_timer_stop(timer);
}

static inline int os_timer_new(os_timer_t *timer,
	void (*func)(void *, void *), void *arg,
	int internal_ms, int repeat, unsigned char auto_run)
{
	return aos_timer_new_ext(timer, func, arg, internal_ms, repeat, auto_run);
}

static inline void os_timer_free(os_timer_t *timer)
{
	aos_timer_free(timer);
}

static inline int os_task_create(os_task_t *task, const char *name,
		void (*fn)(void *), void *arg, int stack_size, int pri)
{
	return aos_task_new_ext(task, name, fn, arg, stack_size, pri);
}

static inline const char *os_partition_name(int pt)
{
	hal_logic_partition_t info;
	memset(&info, 0, sizeof(info));
	if (hal_flash_info_get((hal_partition_t)pt, &info))
		return NULL;
	return info.partition_description;
}

static inline int os_partition_size(int pt)
{
	hal_logic_partition_t info;
	memset(&info, 0, sizeof(info));
	if (hal_flash_info_get((hal_partition_t)pt, &info))
		return -1;
	return info.partition_length;
}

static inline int os_partition_read(int pt, uint32_t *offset, uint8_t *buffer, uint32_t len)
{
	return hal_flash_read((hal_partition_t)pt, offset, buffer, len);
}

static inline int os_partition_write(int pt, uint32_t *offset, const uint8_t *buffer , uint32_t len)
{
	return hal_flash_write((hal_partition_t)pt, offset, buffer, len);
}

static inline int os_partition_erase(int pt, uint32_t offset, uint32_t len)
{
	return hal_flash_erase((hal_partition_t)pt, offset, len);
}


#endif /* __UVOICE_OS_H__ */

